// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.radio.devices;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import se.krka.kahlua.vm.KahluaTable;
import zombie.Lua.LuaManager;

public final class DevicePresets implements Cloneable {
    protected int maxPresets = 10;
    protected ArrayList<PresetEntry> presets = new ArrayList();

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public KahluaTable getPresetsLua() {
        KahluaTable kahluaTable0 = LuaManager.platform.newTable();

        for (int _int = 0; _int < this.presets.size(); _int++) {
            PresetEntry presetEntry = (PresetEntry)this.presets.get(_int);
            KahluaTable kahluaTable1 = LuaManager.platform.newTable();
            kahluaTable1.rawset("name", presetEntry.name);
            kahluaTable1.rawset("frequency", presetEntry.frequency);
            kahluaTable0.rawset(_int, kahluaTable1);
        }

        return kahluaTable0;
    }

    public ArrayList<PresetEntry> getPresets() {
        return this.presets;
    }

    public void setPresets(ArrayList<PresetEntry> p) {
        this.presets = p;
    }

    public int getMaxPresets() {
        return this.maxPresets;
    }

    public void setMaxPresets(int m) {
        this.maxPresets = m;
    }

    public void addPreset(String name, int frequency) {
        if (this.presets.size() < this.maxPresets) {
            this.presets.add(new PresetEntry(name, frequency));
        }
    }

    public void removePreset(int id) {
        if (this.presets.size() != 0 && id >= 0 && id < this.presets.size()) {
            this.presets.remove(id);
        }
    }

    public String getPresetName(int id) {
        return this.presets.size() != 0 && id >= 0 && id < this.presets.size() ? ((PresetEntry)this.presets.get(id)).name : "";
    }

    public int getPresetFreq(int id) {
        return this.presets.size() != 0 && id >= 0 && id < this.presets.size() ? ((PresetEntry)this.presets.get(id)).frequency : -1;
    }

    public void setPresetName(int id, String name) {
        if (name == null) {
            name = "name-is-null";
        }

        if (this.presets.size() != 0 && id >= 0 && id < this.presets.size()) {
            PresetEntry presetEntry = (PresetEntry)this.presets.get(id);
            presetEntry.name = name;
        }
    }

    public void setPresetFreq(int id, int frequency) {
        if (this.presets.size() != 0 && id >= 0 && id < this.presets.size()) {
            PresetEntry presetEntry = (PresetEntry)this.presets.get(id);
            presetEntry.frequency = frequency;
        }
    }

    public void setPreset(int id, String name, int frequency) {
        if (name == null) {
            name = "name-is-null";
        }

        if (this.presets.size() != 0 && id >= 0 && id < this.presets.size()) {
            PresetEntry presetEntry = (PresetEntry)this.presets.get(id);
            presetEntry.name = name;
            presetEntry.frequency = frequency;
        }
    }

    public void clearPresets() {
        this.presets.clear();
    }

    public void save(ByteBuffer output, boolean net) throws IOException {
        output.putInt(this.maxPresets);
        output.putInt(this.presets.size());

        for (int _int = 0; _int < this.presets.size(); _int++) {
            PresetEntry presetEntry = (PresetEntry)this.presets.get(_int);
            zombie.GameWindow.WriteString(output, presetEntry.name);
            output.putInt(presetEntry.frequency);
        }
    }

    public void load(ByteBuffer input, int WorldVersion, boolean net) throws IOException {
        if (WorldVersion >= 69) {
            this.clearPresets();
            this.maxPresets = input.getInt();
            int int0 = input.getInt();

            for (int int1 = 0; int1 < int0; int1++) {
                String string = zombie.GameWindow.ReadString(input);
                int int2 = input.getInt();
                if (this.presets.size() < this.maxPresets) {
                    this.presets.add(new PresetEntry(string, int2));
                }
            }
        }
    }
}
